package com.shensi.appCollect.service.impl;

import com.alibaba.fastjson.JSON;
import com.shensi.appCollect.controller.QuJiaPayV2Controller;
import com.shensi.appCollect.pojo.CustomerDialogueAdTemplate;
import com.shensi.appCollect.pojo.Enterprise;
import com.shensi.appCollect.service.EnterpriseService;
import com.shensi.appCollect.service.WeChatService;
import com.shensi.appCollect.utils.HttpClientUtil;
import com.shensi.appCollect.utils.StringUtils;
import com.shensi.appCollect.utils.exception.EndingException;
import com.shensi.appCollect.utils.redis.RedisCacheUtil;
import com.shensi.appCollect.utils.weixinapp.HttpRequestUtil;
import com.shensi.appCollect.utils.weixinapp.WeiXinUserUtil;
import com.shensi.common.core.constant.RedisKeyPrefixConstant;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Service
public class WeChatServiceImpl implements WeChatService {
    private static Logger logger = (Logger) LoggerFactory.getLogger(WeChatServiceImpl.class);

    @Resource
    private RedisCacheUtil redisCacheUtil;
    @Resource
    private EnterpriseService enterpriseService;


    //客服系统退给用户消息 CUSTOMER_DIALOGUE
    private static final String CUSTOMER_DIALOGUE_RUL = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";

    public synchronized String getAccessToken(String appId, String appSecret) {
        //redis缓存key
        String accessTokenKey = "access_token_" + appId;
        if (!redisCacheUtil.hasKey(accessTokenKey) || StringUtils.isBlank(redisCacheUtil.getCacheObject(accessTokenKey))) {
            String token = WeiXinUserUtil.getToken(appId, appSecret).getAccessToken();
            redisCacheUtil.setCacheObject(accessTokenKey, token);
            redisCacheUtil.expire(accessTokenKey, 7100, TimeUnit.SECONDS);
            logger.info("getTokenByRedisCache-set appId " + appId + " 's token is " + token);
            return token;
        } else {
            String token = redisCacheUtil.getCacheObject(accessTokenKey);
            logger.info("getTokenByRedisCache-get appId " + appId + " 's token is " + token);
            return token;
        }
    }

    /*
     * 客服对话框推送消息
     * @param accessToken
     * @return
     */
    public void customerDialogue(String originId, String openId) {
        try {
            logger.info("--------------------------");
            Enterprise enterprise = enterpriseService.selectEnterpriseByOriginId(originId);
            if (enterprise == null || StringUtils.isBlank(enterprise.getAppId()) || StringUtils.isBlank(enterprise.getAppSecret())) {
                throw new EndingException("该油企未查到或未配置相关参数");
            }
            HashMap<String, Object> paramsHashMap = new HashMap<>();
            paramsHashMap.put("touser", openId);

            Long dialogueAdId = enterprise.getDialogueAdId();
            if (dialogueAdId == null) {
                throw new EndingException("油企未配置adId");
            }
            CustomerDialogueAdTemplate customerDialogueAdTemplate = enterpriseService.selectCustomerDialogueAdTemplateById(dialogueAdId);

            if (customerDialogueAdTemplate == null) {
                throw new EndingException("未查到ad模板");
            }
            String type = customerDialogueAdTemplate.getType();
            logger.info(customerDialogueAdTemplate.toString());
            if (StringUtils.isBlank(type)) {
                throw new EndingException("ad模板未配置type");
            }


            if ("text".equals(type)) {
                HashMap<String, Object> textHashMap = new HashMap<>();
                textHashMap.put("content", customerDialogueAdTemplate.getContent());

                paramsHashMap.put("msgtype", "text");
                paramsHashMap.put("text", textHashMap);
            } else if ("image".equals(type)) {
                HashMap<String, Object> imageHashMap = new HashMap<>();
                imageHashMap.put("media_id", customerDialogueAdTemplate.getMediaId());

                paramsHashMap.put("msgtype", "image");
                paramsHashMap.put("image", imageHashMap);
            } else if ("link".equals(type)) {
                HashMap<String, Object> linkHashMap = new HashMap<>();
                linkHashMap.put("title", customerDialogueAdTemplate.getTitle());
                linkHashMap.put("description", customerDialogueAdTemplate.getDescription());
                linkHashMap.put("url", customerDialogueAdTemplate.getUrl());
                linkHashMap.put("thumb_url", customerDialogueAdTemplate.getThumbUrl());

                paramsHashMap.put("msgtype", "link");
                paramsHashMap.put("link", linkHashMap);
            } else if ("miniprogrampage".equals(type)) {
                HashMap<String, Object> miniHashMap = new HashMap<>();
                miniHashMap.put("title", customerDialogueAdTemplate.getTitle());
                miniHashMap.put("pagepath", customerDialogueAdTemplate.getPagepath());
                miniHashMap.put("thumb_media_id", customerDialogueAdTemplate.getThumbMediaId());

                paramsHashMap.put("msgtype", "miniprogrampage");
                paramsHashMap.put("miniprogrampage", miniHashMap);
            } else {
                throw new EndingException("type类型不对");
            }


            String accessToken = this.getAccessToken(enterprise.getAppId(), enterprise.getAppSecret());
            if (StringUtils.isBlank(accessToken)) {
                throw new EndingException("accessToken获取失败");
            }
            logger.info("accessToken:" + accessToken);
            String requestUrl = CUSTOMER_DIALOGUE_RUL.replace("ACCESS_TOKEN", accessToken);
            JSONObject resultJson = HttpClientUtil.doPostJson(requestUrl, JSON.toJSONString(paramsHashMap));


            logger.info("resultJson：" + resultJson);
            logger.info("resultJson：" + resultJson.get("errcode"));
            logger.info("resultJson：" + resultJson.get("errmsg"));

            logger.info("--------------------------");
        } catch (EndingException e) {
            logger.info("customerDialogue-" + e.getMessage(), e);
        } catch (Exception e) {
            logger.info("customerDialogue-", e);
        }

    }
}

